package com.xgkj.diyiketang.fragment;


import android.Manifest;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;
import com.xgkj.diyiketang.EatShowApp;
import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.BangDingPhoneActivity;
import com.xgkj.diyiketang.activity.MainActivity;
import com.xgkj.diyiketang.activity.data.LiveListFirstPageAdapter;
import com.xgkj.diyiketang.activity.data.SubjectAdapter;
import com.xgkj.diyiketang.activity.data.XueXiaoAdapter;
import com.xgkj.diyiketang.data.AdvertiseInfo;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.data.SchoolInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.http.engine.TaskResultCode;
import com.xgkj.diyiketang.task.AdvertisementTask;
import com.xgkj.diyiketang.task.GetUserDetailTask;
import com.xgkj.diyiketang.task.LiveListTask;
import com.xgkj.diyiketang.util.DialogUtil;
import com.xgkj.diyiketang.util.NoDataUtil;
import com.xgkj.diyiketang.util.PopupHelper;
import com.xgkj.diyiketang.util.SharedUtils;
import com.xgkj.diyiketang.util.ToastUtil;
import com.xgkj.diyiketang.weidget.PullToRefreshListView;
import com.xgkj.diyiketang.yizhibo.Constants;
import com.xgkj.diyiketang.yizhibo.Preferences;
import com.xgkj.diyiketang.yizhibo.RecorderNewActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class ZhiboFirstPageFragment extends BaseFragment implements PullToRefreshListView.OnRefreshListener, PullToRefreshListView.OnLoadDataListener, View.OnClickListener, PullToRefreshListView.OnScrollListener {
    private Context mContext;
    private View view;
    private TextView mToZhibo;
    private String canLive;
    private PullToRefreshListView mListView;
    private LiveListFirstPageAdapter mAdapter;
    private int currentPage = 1;
    private int size = 10;
    private int mTuijianListTaskId = -1;
    private int mZhiboType;
    private String versionCode;
    private LinearLayout xuanzeLayout;
    private RelativeLayout chooseRegion;
    private TextView region;
    private RelativeLayout chooseKemu;
    private TextView kemu;
    private RelativeLayout choosePaixu;
    private TextView paixu;
    private RelativeLayout chooseLeiBie;
    private TextView leibie, first_hot, first_new;
    private String mLeiBie;
    private PopupHelper mLeiBiePopWindow;
    private PopupHelper mRegionPopWindow;
    private PopupHelper mSubjectPopWindow;
    private PopupHelper mOrderPopWindow;
    private SubjectAdapter mSubjectAdapter;
    private SubjectAdapter mOrderAdapter;
    private XueXiaoAdapter mXuexiaoAdapter;
    private SubjectAdapter mLeibieAdapter;
    private SubjectAdapter mRegionAdapter;
    private String mSubjectNo;
    private String mRegion;
    private NoDataUtil noDataUtil;
    private ImageView zhiboSearch;
    private int schoolListTaskId = -1;
    private int detailTaskId = -1;
    EatShowUserInfo userInfo;
    private int advertiseId = -1;
    //private ImageCycleView adView;
    private List<AdvertiseInfo> adList = new ArrayList<AdvertiseInfo>();
    private int firstListItem;
    private boolean isShow = false;
    private int isLive = -1;
    private String canStartLive;
    private View empty_view;
    private String fansNum = "0";
    private PopupWindow firstSetPop;
    private List<EatShowUserInfo> fansList = new ArrayList<EatShowUserInfo>();
    boolean isFirst = true;


    public ZhiboFirstPageFragment() {
        // Required empty public constructor
    }


    @Override
    protected int getContentViewId() {
        return 0;
    }

    /**
     * 6.0权限处理
     **/
    private boolean bPermission = false;
    private final int WRITE_PERMISSION_REQ_CODE = 100;

    private boolean checkPublishPermission() {
        if (Build.VERSION.SDK_INT >= 23) {
            List<String> permissions = new ArrayList<>();
            if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
            }
            if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA)) {
                permissions.add(Manifest.permission.CAMERA);
            }
            if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.RECORD_AUDIO)) {
                permissions.add(Manifest.permission.RECORD_AUDIO);
            }
            if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_PHONE_STATE)) {
                permissions.add(Manifest.permission.READ_PHONE_STATE);
            }
            if (permissions.size() != 0) {
                ActivityCompat.requestPermissions(getActivity(),
                        (String[]) permissions.toArray(new String[0]),
                        WRITE_PERMISSION_REQ_CODE);
                return false;
            }
        }
        return true;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case WRITE_PERMISSION_REQ_CODE:
                for (int ret : grantResults) {
                    if (ret != PackageManager.PERMISSION_GRANTED) {
                        return;
                    }
                }
                bPermission = true;
                break;
            default:
                break;
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        if (view == null) {
            view = inflater.inflate(R.layout.zhibofragment, null);
        }

        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null) {
            parent.removeView(view);
        }
        mContext = getActivity();
//        bPermission = checkPublishPermission();
        initView(view);
        getUserInfoIsLive();
        checkAppKey(Constants.APP_KEY_ID);
        return view;
    }


    /*直播的 判断方法请求*/
    public void getUserInfoIsLive() {
        GetUserDetailTask task = new GetUserDetailTask();
        task.addListener(new ForgetUserInfoTaskEventListener(mContext));
        isLive = getEatShowApp().getTaskManager().addTask(task);

    }

    class ForgetUserInfoTaskEventListener extends ShowToastTaskEventListener {
        public ForgetUserInfoTaskEventListener(Context context) {
            super(context);
        }

        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if (taskId == isLive) {
                isLive = -1;

                // EatShowApp data1 = (EatShowApp) data;
                EatShowUserInfo data1 = (EatShowUserInfo) data;
                canStartLive = data1.getCanStartLive();
                //通过sp存储，对vip等级进行存储
                if (data1.isVip() >= 0) {
                    int vip = data1.isVip();
                    SharedUtils.putInt("vip", vip);
                }
                if (null != data1.getFans() && "0" == fansNum) {
                    fansNum = data1.getFans();
                }
                Log.e("canStartLive1::", canStartLive);
                if (getEatShowApp().getmUserInfo() != null) {
                    getEatShowApp().getmUserInfo().setCanStartLive(canStartLive);

                }
            }
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
        }
    }

    private void checkAppKey(String appkey) {
        String cachedAppKey = Preferences.getInstance(mContext.getApplicationContext()).getString(Preferences.KEY_APP_KEY);
        if (TextUtils.isEmpty(cachedAppKey) || !cachedAppKey.equals(appkey)) {
            Preferences.getInstance(mContext.getApplicationContext()).putString(Preferences.KEY_APP_KEY, appkey);
            Preferences.getInstance(mContext.getApplicationContext()).remove(Preferences.KEY_SESSION_ID);
            Preferences.getInstance(mContext.getApplicationContext()).remove(Preferences.KEY_USER_NUMBER);
            Preferences.getInstance(mContext.getApplicationContext()).remove(Preferences.KEY_USER_NICKNAME);
        }
    }


    private RelativeLayout first_list_rl;

    private void initView(View view) {
        getAdList();
        try {
            PackageInfo packageInfo = mContext.getPackageManager().getPackageInfo(mContext.getPackageName(), 0);
            versionCode = packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        zhiboSearch = (ImageView) view.findViewById(R.id.zhibo_search);
        zhiboSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setPopWindow();
//                startActivity(new Intent(mContext, SearchZhiboActivity.class));
            }
        });
        first_list_rl = (RelativeLayout) view.findViewById(R.id.first_list_rl);
        first_hot = (TextView) view.findViewById(R.id.zhibo_first_hot);
        first_hot.setOnClickListener(this);
        first_new = (TextView) view.findViewById(R.id.zhibo_first_new);
        first_new.setOnClickListener(this);
        xuanzeLayout = (LinearLayout) view.findViewById(R.id.xuanze_layout);
        chooseLeiBie = (RelativeLayout) view.findViewById(R.id.choose_leibie);
        leibie = (TextView) view.findViewById(R.id.leibie);
        chooseLeiBie.setOnClickListener(this);
        chooseRegion = (RelativeLayout) view.findViewById(R.id.choose_region);
        chooseRegion.setOnClickListener(this);
        region = (TextView) view.findViewById(R.id.region);
        chooseKemu = (RelativeLayout) view.findViewById(R.id.choose_kemu);
        chooseKemu.setOnClickListener(this);
        kemu = (TextView) view.findViewById(R.id.kemu);
        choosePaixu = (RelativeLayout) view.findViewById(R.id.choose_paixu);
        choosePaixu.setOnClickListener(this);
        paixu = (TextView) view.findViewById(R.id.paixu);
        View view1 = view.findViewById(R.id.content_layout);
        noDataUtil = new NoDataUtil(mContext, view1);
        mListView = (PullToRefreshListView) view.findViewById(R.id.zhibo_list);
        mListView.setOnScrollListener(this);
        mListView.init(mContext, true);
        mAdapter = new LiveListFirstPageAdapter(mContext);
        mAdapter.setOnAdapterClickListener(this);
        mListView.setAdapter(mAdapter, true);
        mListView.setOnRefreshListener(this);
        mListView.setOnLoadDataListener(this);
        mToZhibo = (TextView) view.findViewById(R.id.zhibo_start);
//        mToZhibo.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
////                if(!bPermission){
////                    Toast.makeText(getActivity(),"请先允许app所需要的权限", Toast.LENGTH_LONG).show();
////                    bPermission = checkPublishPermission();
////                    return;
////                }
//
////                if(StringUtil.isEmpty(publishParam.pushUrl) || !publishParam.pushUrl.contains(".live.126.net")){
////                    Toast.makeText(getApplication(),"请先设置正确的推流地址", Toast.LENGTH_LONG).show();
////                }
////                else
////                {
//                    Intent intent = new Intent(getActivity(), LiveStreamingActivity.class);
////                    intent.putExtra("data", publishParam);
//                    startActivity(intent);
////                }
//
//            }
//        });
        mToZhibo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtil.createDialog(mContext, -1, mContext.getString(R.string.prompt),
                        mContext.getString(R.string.qingxuanze), mContext.getString(R.string.quyuyue),
                        mContext.getString(R.string.quzhibo), new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                if (Integer.parseInt(canLive) == 1) {
                                    Intent yintent = new Intent(mContext, RecorderNewActivity.class);
                                    yintent.putExtra("FROM", "YUYUE");
                                    yintent.putExtra("FANSNUM", Integer.parseInt(fansNum));
                                    startActivity(yintent);
                                } else {
                                    Intent bangding = new Intent(mContext, BangDingPhoneActivity.class);
                                    startActivity(bangding);
                                }
                            }
                        }, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                if (Integer.parseInt(canLive) == 1) {
                                    Intent zintent = new Intent(mContext, RecorderNewActivity.class);
                                    zintent.putExtra("FROM", "ZHIBO");
                                    zintent.putExtra("FANSNUM", Integer.parseInt(fansNum));
                                    startActivity(zintent);
                                } else {
                                    Intent bangding = new Intent(mContext, BangDingPhoneActivity.class);
                                    startActivity(bangding);
                                }
                            }
                        });

            }
        });


        initPopWindow();
        //TODO
        mZhiboType = 2;
        mListView.doRefresh();
    }

    private void initPopWindow() {
        //类别popwindow
        mLeibieAdapter = new SubjectAdapter(mContext, 5);
        mLeibieAdapter.setSelectPosition(0);
        leibie.setText((String) mLeibieAdapter.getItem(0));
        mLeiBiePopWindow = new PopupHelper(mContext, mLeibieAdapter);
        mLeiBiePopWindow.setOnDisMissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                leibie.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.smallxiajiantou, 0);
            }
        });

        mLeiBiePopWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mLeiBiePopWindow.dismissDropDownView();
                if (position == 0) {
                    mLeiBie = null;
                } else {
//                    mLeiBie = String.valueOf(position - 1);
                    if (String.valueOf(position - 1).equals("0")) {
                        mLeiBie = "992a1f76047e4839a9631259c033d867";
                    } else if (String.valueOf(position - 1).equals("1")) {
                        mLeiBie = "a10e3a144fd6412598c18796951386c1";
                    } else if (String.valueOf(position - 1).equals("2")) {
                        mLeiBie = "d74604615a344df3bb4c569a40e16d28";
                    } else if (String.valueOf(position - 1).equals("3")) {
                        mLeiBie = "e9fa420b2d774965b80b48727fe3e968";
                    } else if (String.valueOf(position - 1).equals("4")) {
                        mLeiBie = "31d41764f6a24099a04bc1b614289f2c";
                    } else if (String.valueOf(position - 1).equals("5")) {
                        mLeiBie = "fe45e51fee0f4d43998490cb675a9fa4";
                    }
                }
                String currentLeiBie = (String) mLeibieAdapter.getItem(position);
                leibie.setText(currentLeiBie);
                mAdapter.setCurrentLeibieType(currentLeiBie);
                mLeibieAdapter.setSelectPosition(position);
                mListView.doRefresh();
            }
        });

        //地区
        mRegionAdapter = new SubjectAdapter(mContext, 6);
        mRegionAdapter.setSelectPosition(0);
        region.setText((String) mRegionAdapter.getItem(0));
        mRegionPopWindow = new PopupHelper(mContext, mRegionAdapter);
        mRegionPopWindow.setOnDisMissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                region.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.smallxiajiantou, 0);
            }
        });
        //df
        mRegionPopWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mRegionPopWindow.dismissDropDownView();
                if (position == 0) {
                    mRegion = null;
                } else {
                    mRegion = String.valueOf(position - 1);
                }
                String currentLeiBie = (String) mRegionAdapter.getItem(position);
                region.setText(currentLeiBie);
                mAdapter.setCurrentLeibieType(currentLeiBie);
                mRegionAdapter.setSelectPosition(position);
                mListView.doRefresh();
            }
        });


        //科目popupwindow
        mSubjectAdapter = new SubjectAdapter(mContext, 1);
        mSubjectAdapter.setSelectPosition(0);
        kemu.setText((String) mSubjectAdapter.getItem(0));
        mSubjectPopWindow = new PopupHelper(mContext, mSubjectAdapter);
        mSubjectPopWindow.setOnDisMissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                kemu.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.smallxiajiantou, 0);
            }
        });
        mSubjectPopWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mSubjectPopWindow.dismissDropDownView();
                if (position == 0) {
                    mSubjectNo = null;
                } else {
                    mSubjectNo = String.valueOf(position - 1);
                }

                String currentKemu = (String) mSubjectAdapter.getItem(position);
                kemu.setText(currentKemu);
                mAdapter.setCurrentKemuType(currentKemu);
                mSubjectAdapter.setSelectPosition(position);
                mListView.doRefresh();
            }
        });

        //直播类型最新，最热，关注
        mOrderAdapter = new SubjectAdapter(mContext, 4);
        mOrderAdapter.setSelectPosition(0);
        mOrderPopWindow = new PopupHelper(mContext, mOrderAdapter);
        mOrderPopWindow.setOnDisMissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                paixu.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.smallxiajiantou, 0);
            }
        });
        mOrderPopWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mOrderPopWindow.dismissDropDownView();
                mZhiboType = position + 1;
//                orderNo = String.valueOf(position);
                String currentOrder = (String) mOrderAdapter.getItem(position);
                paixu.setText(currentOrder);
                mAdapter.setCurrentOrder(currentOrder);
                mOrderAdapter.setSelectPosition(position);
                mListView.doRefresh();
            }
        });
    }

    class MyListener extends ShowToastTaskEventListener {
        public MyListener(Context context) {
            super(context);
        }

        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if (taskId == mTuijianListTaskId) {
                mTuijianListTaskId = -1;
//                if (mListView.getVisibility() == View.VISIBLE) {
                if (currentPage == 1) {
                    mAdapter.clearData();
                    isFirst = true;
                    mListView.onRefreshComplete();
                    mListView.enableLoadMore();
                    mListView.setSelection(0);
                } else {
                    mListView.onLoadDataComplete(true);
                }
                if (data != null) {
                    fansList = (List<EatShowUserInfo>) data;
                }
                if (fansList != null && fansList.size() > 0 && isFirst) {
                    fansList.add(0, new EatShowUserInfo());
                    isFirst = false;
                }
                mAdapter.updateData(fansList, mZhiboType);

                if (mAdapter != null && mAdapter.getCount() > 0) {
                    noDataUtil.ListViewNoEmpty(mListView);
                } else {
//                    xuanzeLayout.setVisibility(View.VISIBLE);
                    noDataUtil.ListViewEmpty(mListView, null, null, -1);
                }
                currentPage = result;
            } else if (taskId == schoolListTaskId) {
                if (data != null) {
                    List<SchoolInfo> schoolInfos = (List<SchoolInfo>) data;
                    if (schoolInfos != null && schoolInfos.size() > 0) {
                        mXuexiaoAdapter.clearData();
                        mXuexiaoAdapter.updateData(schoolInfos);
                    }
                }
            } else if (taskId == detailTaskId) {
                if (data != null) {
                    userInfo = (EatShowUserInfo) data;
                    if (userInfo != null) {
                        canLive = userInfo.getCanStartLive();
                    }
                }
            } else if (taskId == advertiseId) {
                advertiseId = -1;

                if (data != null) {
                    adList = (List<AdvertiseInfo>) data;
                    if (adList != null && adList.size() > 0) {
                        mAdapter.addImageView(adList);
                        mAdapter.notifyDataSetChanged();
                    }

                }
            }

        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            if (errorCode == TaskResultCode.NEED_LOGIN) {
                ((MainActivity) getActivity()).showFirstPage();
            }
            super.handleTaskError(taskId, errorCode, errorMsg);
            if (taskId == mTuijianListTaskId) {
                mTuijianListTaskId = -1;
                if (currentPage == 1) {
                    mListView.onRefreshComplete();
                } else {
                    mListView.onLoadDataComplete(false);
                }
            }
        }

        @Override
        public void handleTaskMessage(int taskId, Object data) {
            super.handleTaskMessage(taskId, data);
        }
    }

    private void getUserDetail() {
        GetUserDetailTask task = new GetUserDetailTask();
        task.addListener(new MyListener(mContext));
        detailTaskId = getEatShowApp().getTaskManager().addTask(task);
    }

    private void getTuijianList() {
        if (mTuijianListTaskId != -1) {
            return;
        }
        if (currentPage == 0) {
            mListView.setNoNextPage();
            return;
        }
        LiveListTask task = new LiveListTask(size, currentPage, null, null, mZhiboType, versionCode, mSubjectNo, "", mRegion, mLeiBie, "0", "1");
        task.addListener(new MyListener(mContext));
        mTuijianListTaskId = getEatShowApp().getTaskManager().addTask(task);
    }

    private void getAdList() {
        AdvertisementTask adtask = new AdvertisementTask("3", "7");
        adtask.addListener(new MyListener(mContext));
        advertiseId = getEatShowApp().getTaskManager().addTask(adtask);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        getUserDetail();
        if (!hidden) {
            isShow = true;
            if (mAdapter != null && mAdapter.getCount() == 0) {
                mListView.doRefresh();
            }
        } else {
            isShow = false;
        }
    }

    private String subject = "0", grade = "1";
    private String xueji = "1";

    @Override
    public void onClick(View v) {
        if (firstListItem <= 0) {
            if (mListView.getHeaderViewsCount() > 0) {
                mListView.post(new Runnable() {
                    @Override
                    public void run() {
                        mListView.setSelection(1);
                    }
                });

            } else {
                mListView.smoothScrollToPosition(1);
            }
        }
        switch (v.getId()) {
            case R.id.choose_region:
                region.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.smallshangjiantou, 0);
                mRegionPopWindow.showDropDownView(v);
                break;
            case R.id.choose_kemu:
                kemu.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.smallshangjiantou, 0);
                mSubjectPopWindow.showDropDownView(v);
                break;
            case R.id.choose_paixu:
                paixu.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.smallshangjiantou, 0);
                mOrderPopWindow.showDropDownView(v);
                break;
            case R.id.choose_leibie:
                leibie.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.smallshangjiantou, 0);
                mLeiBiePopWindow.showDropDownView(v);
                break;
            case R.id.zhibo_first_hot:
                first_hot.setBackgroundResource(R.drawable.zhibo_first_title);
                first_new.setBackgroundResource(R.drawable.zhibo_first_title2);
                first_new.setTextColor(getResources().getColor(R.color.shouye_tab_color_selected));
                first_hot.setTextColor(getResources().getColor(R.color.white));
                mZhiboType = 2;
                currentPage = 1;
                noDataUtil.ListViewNoEmpty(mListView);
                getTuijianList();
                mListView.doRefresh();
                break;
            case R.id.zhibo_first_new:
                first_hot.setBackgroundResource(R.drawable.zhibo_first_titlepre);
                first_new.setBackgroundResource(R.drawable.zhibo_first_title2_pre);
                first_hot.setTextColor(getResources().getColor(R.color.shouye_tab_color_selected));
                first_new.setTextColor(getResources().getColor(R.color.white));
                mZhiboType = 1;
                currentPage = 1;
                noDataUtil.ListViewNoEmpty(mListView);
                getTuijianList();
                mListView.doRefresh();
                break;
            case R.id.xiaoxue:
                nianji_buttom.setVisibility(View.VISIBLE);
                xiaoxue.setBackgroundResource(R.drawable.zhibo_sey_btn_xuanzhong);
                chuzhong.setBackgroundResource(R.drawable.chazhao_pop_btn);
                gaozhong.setBackgroundResource(R.drawable.chazhao_pop_btn);
                chuzhong.setTextColor(getResources().getColor(R.color.shouye_tab_color_selected));
                gaozhong.setTextColor(getResources().getColor(R.color.shouye_tab_color_selected));
                xiaoxue.setTextColor(getResources().getColor(R.color.white));
                yinianji.setText("一年级");
                ernianji.setText("二年级");
                sannianji.setText("三年级");
                sinianji.setText("四年级");
                wunianji.setText("五年级");
                liunianji.setText("六年级");
                xueji = "1";
                break;
            case R.id.chuzhong:
                xiaoxue.setBackgroundResource(R.drawable.chazhao_pop_btn);
                chuzhong.setBackgroundResource(R.drawable.zhibo_sey_btn_xuanzhong);
                gaozhong.setBackgroundResource(R.drawable.chazhao_pop_btn);
                chuzhong.setTextColor(getResources().getColor(R.color.white));
                gaozhong.setTextColor(getResources().getColor(R.color.shouye_tab_color_selected));
                xiaoxue.setTextColor(getResources().getColor(R.color.shouye_tab_color_selected));
                yinianji.setText("初一");
                ernianji.setText("初二");
                sannianji.setText("初三");
                xueji = "2";
                nianji_buttom.setVisibility(View.GONE);
                break;
            case R.id.gaozhong:
                xiaoxue.setBackgroundResource(R.drawable.chazhao_pop_btn);
                chuzhong.setBackgroundResource(R.drawable.chazhao_pop_btn);
                gaozhong.setBackgroundResource(R.drawable.zhibo_sey_btn_xuanzhong);
                chuzhong.setTextColor(getResources().getColor(R.color.shouye_tab_color_selected));
                gaozhong.setTextColor(getResources().getColor(R.color.white));
                xiaoxue.setTextColor(getResources().getColor(R.color.shouye_tab_color_selected));
                yinianji.setText("高一");
                ernianji.setText("高二");
                sannianji.setText("高三");
                xueji = "3";
                nianji_buttom.setVisibility(View.GONE);
                break;
            case R.id.yuwen:
                yingyu.setBackgroundResource(R.drawable.chazhao_pop_btn);
                zonghe.setBackgroundResource(R.drawable.chazhao_pop_btn);
                shuxue.setBackgroundResource(R.drawable.chazhao_pop_btn);
                yuwen.setBackgroundResource(R.drawable.zhibo_sey_btn_xuanzhong);
                yingyu.setTextColor(getResources().getColor(R.color.shouye_tab_color_selected));
                yuwen.setTextColor(getResources().getColor(R.color.white));
                zonghe.setTextColor(getResources().getColor(R.color.shouye_tab_color_selected));
                shuxue.setTextColor(getResources().getColor(R.color.shouye_tab_color_selected));
                subject = "0";
                break;
            case R.id.shuxue:
                yingyu.setBackgroundResource(R.drawable.chazhao_pop_btn);
                zonghe.setBackgroundResource(R.drawable.chazhao_pop_btn);
                yuwen.setBackgroundResource(R.drawable.chazhao_pop_btn);
                shuxue.setBackgroundResource(R.drawable.zhibo_sey_btn_xuanzhong);
                yingyu.setTextColor(getResources().getColor(R.color.shouye_tab_color_selected));
                yuwen.setTextColor(getResources().getColor(R.color.shouye_tab_color_selected));
                zonghe.setTextColor(getResources().getColor(R.color.shouye_tab_color_selected));
                shuxue.setTextColor(getResources().getColor(R.color.white));
                subject = "1";
                break;
            case R.id.yingyu:
                yingyu.setBackgroundResource(R.drawable.zhibo_sey_btn_xuanzhong);
                zonghe.setBackgroundResource(R.drawable.chazhao_pop_btn);
                shuxue.setBackgroundResource(R.drawable.chazhao_pop_btn);
                yuwen.setBackgroundResource(R.drawable.chazhao_pop_btn);
                yingyu.setTextColor(getResources().getColor(R.color.white));
                yuwen.setTextColor(getResources().getColor(R.color.shouye_tab_color_selected));
                zonghe.setTextColor(getResources().getColor(R.color.shouye_tab_color_selected));
                shuxue.setTextColor(getResources().getColor(R.color.shouye_tab_color_selected));
                subject = "2";
                break;
            case R.id.zonghe:
                yingyu.setBackgroundResource(R.drawable.chazhao_pop_btn);
                zonghe.setBackgroundResource(R.drawable.zhibo_sey_btn_xuanzhong);
                shuxue.setBackgroundResource(R.drawable.chazhao_pop_btn);
                yuwen.setBackgroundResource(R.drawable.chazhao_pop_btn);
                yingyu.setTextColor(getResources().getColor(R.color.shouye_tab_color_selected));
                yuwen.setTextColor(getResources().getColor(R.color.shouye_tab_color_selected));
                zonghe.setTextColor(getResources().getColor(R.color.white));
                shuxue.setTextColor(getResources().getColor(R.color.shouye_tab_color_selected));
                subject = "";
                break;
            case R.id.yinianji:
                yinianji.setBackgroundResource(R.drawable.zhibo_sey_btn_xuanzhong);
                ernianji.setBackgroundResource(R.drawable.chazhao_pop_btn);
                sannianji.setBackgroundResource(R.drawable.chazhao_pop_btn);
                sinianji.setBackgroundResource(R.drawable.chazhao_pop_btn);
                wunianji.setBackgroundResource(R.drawable.chazhao_pop_btn);
                liunianji.setBackgroundResource(R.drawable.chazhao_pop_btn);
                yinianji.setTextColor(getResources().getColor(R.color.white));
                ernianji.setTextColor(getResources().getColor(R.color.shouye_tab_color_selected));
                sannianji.setTextColor(getResources().getColor(R.color.shouye_tab_color_selected));
                sinianji.setTextColor(getResources().getColor(R.color.shouye_tab_color_selected));
                wunianji.setTextColor(getResources().getColor(R.color.shouye_tab_color_selected));
                liunianji.setTextColor(getResources().getColor(R.color.shouye_tab_color_selected));
                if (xueji.equals("1")) {
                    yinianji.setText("一年级");
                    ernianji.setText("二年级");
                    sannianji.setText("三年级");
                    sinianji.setText("四年级");
                    wunianji.setText("五年级");
                    liunianji.setText("六年级");

                } else if (xueji.equals("2")) {
                    nianji_buttom.setVisibility(View.GONE);
                    yinianji.setText("初一");
                    ernianji.setText("初二");
                    sannianji.setText("初三");

                } else if (xueji.equals("3")) {
                    nianji_buttom.setVisibility(View.GONE);
                    yinianji.setText("高一");
                    ernianji.setText("高二");
                    sannianji.setText("高三");
                }

                break;
            case R.id.ernianji:
                yinianji.setBackgroundResource(R.drawable.chazhao_pop_btn);
                ernianji.setBackgroundResource(R.drawable.zhibo_sey_btn_xuanzhong);
                sannianji.setBackgroundResource(R.drawable.chazhao_pop_btn);
                sinianji.setBackgroundResource(R.drawable.chazhao_pop_btn);
                wunianji.setBackgroundResource(R.drawable.chazhao_pop_btn);
                liunianji.setBackgroundResource(R.drawable.chazhao_pop_btn);
                yinianji.setTextColor(getResources().getColor(R.color.shouye_tab_color_selected));
                ernianji.setTextColor(getResources().getColor(R.color.white));
                sannianji.setTextColor(getResources().getColor(R.color.shouye_tab_color_selected));
                sinianji.setTextColor(getResources().getColor(R.color.shouye_tab_color_selected));
                wunianji.setTextColor(getResources().getColor(R.color.shouye_tab_color_selected));
                liunianji.setTextColor(getResources().getColor(R.color.shouye_tab_color_selected));
                if (xueji.equals("1")) {
                    yinianji.setText("一年级");
                    ernianji.setText("二年级");
                    sannianji.setText("三年级");
                    sinianji.setText("四年级");
                    wunianji.setText("五年级");
                    liunianji.setText("六年级");

                } else if (xueji.equals("2")) {
                    nianji_buttom.setVisibility(View.GONE);
                    yinianji.setText("初一");
                    ernianji.setText("初二");
                    sannianji.setText("初三");

                } else if (xueji.equals("3")) {
                    nianji_buttom.setVisibility(View.GONE);
                    yinianji.setText("高一");
                    ernianji.setText("高二");
                    sannianji.setText("高三");
                }
                break;
            case R.id.sannianji:
                yinianji.setBackgroundResource(R.drawable.chazhao_pop_btn);
                ernianji.setBackgroundResource(R.drawable.chazhao_pop_btn);
                sannianji.setBackgroundResource(R.drawable.zhibo_sey_btn_xuanzhong);
                sinianji.setBackgroundResource(R.drawable.chazhao_pop_btn);
                wunianji.setBackgroundResource(R.drawable.chazhao_pop_btn);
                liunianji.setBackgroundResource(R.drawable.chazhao_pop_btn);
                yinianji.setTextColor(getResources().getColor(R.color.shouye_tab_color_selected));
                ernianji.setTextColor(getResources().getColor(R.color.shouye_tab_color_selected));
                sannianji.setTextColor(getResources().getColor(R.color.white));
                sinianji.setTextColor(getResources().getColor(R.color.shouye_tab_color_selected));
                wunianji.setTextColor(getResources().getColor(R.color.shouye_tab_color_selected));
                liunianji.setTextColor(getResources().getColor(R.color.shouye_tab_color_selected));
                if (xueji.equals("1")) {
                    yinianji.setText("一年级");
                    ernianji.setText("二年级");
                    sannianji.setText("三年级");
                    sinianji.setText("四年级");
                    wunianji.setText("五年级");
                    liunianji.setText("六年级");
                } else if (xueji.equals("2")) {
                    nianji_buttom.setVisibility(View.GONE);
                    yinianji.setText("初一");
                    ernianji.setText("初二");
                    sannianji.setText("初三");

                } else if (xueji.equals("3")) {
                    nianji_buttom.setVisibility(View.GONE);
                    yinianji.setText("高一");
                    ernianji.setText("高二");
                    sannianji.setText("高三");
                }
                break;
            case R.id.sinianji:
                yinianji.setBackgroundResource(R.drawable.chazhao_pop_btn);
                ernianji.setBackgroundResource(R.drawable.chazhao_pop_btn);
                sannianji.setBackgroundResource(R.drawable.chazhao_pop_btn);
                sinianji.setBackgroundResource(R.drawable.zhibo_sey_btn_xuanzhong);
                wunianji.setBackgroundResource(R.drawable.chazhao_pop_btn);
                liunianji.setBackgroundResource(R.drawable.chazhao_pop_btn);
                yinianji.setTextColor(getResources().getColor(R.color.shouye_tab_color_selected));
                ernianji.setTextColor(getResources().getColor(R.color.shouye_tab_color_selected));
                sannianji.setTextColor(getResources().getColor(R.color.shouye_tab_color_selected));
                sinianji.setTextColor(getResources().getColor(R.color.white));
                wunianji.setTextColor(getResources().getColor(R.color.shouye_tab_color_selected));
                liunianji.setTextColor(getResources().getColor(R.color.shouye_tab_color_selected));
                if (xueji.equals("1")) {
                    yinianji.setText("一年级");
                    ernianji.setText("二年级");
                    sannianji.setText("三年级");
                    sinianji.setText("四年级");
                    wunianji.setText("五年级");
                    liunianji.setText("六年级");
                } else if (xueji.equals("2")) {
                    nianji_buttom.setVisibility(View.GONE);
                    yinianji.setText("初一");
                    ernianji.setText("初二");
                    sannianji.setText("初三");

                } else if (xueji.equals("3")) {
                    nianji_buttom.setVisibility(View.GONE);
                    yinianji.setText("高一");
                    ernianji.setText("高二");
                    sannianji.setText("高三");
                }
                break;
            case R.id.wunianji:
                yinianji.setBackgroundResource(R.drawable.chazhao_pop_btn);
                ernianji.setBackgroundResource(R.drawable.chazhao_pop_btn);
                sannianji.setBackgroundResource(R.drawable.chazhao_pop_btn);
                sinianji.setBackgroundResource(R.drawable.chazhao_pop_btn);
                wunianji.setBackgroundResource(R.drawable.zhibo_sey_btn_xuanzhong);
                liunianji.setBackgroundResource(R.drawable.chazhao_pop_btn);
                yinianji.setTextColor(getResources().getColor(R.color.shouye_tab_color_selected));
                ernianji.setTextColor(getResources().getColor(R.color.shouye_tab_color_selected));
                sannianji.setTextColor(getResources().getColor(R.color.shouye_tab_color_selected));
                sinianji.setTextColor(getResources().getColor(R.color.shouye_tab_color_selected));
                wunianji.setTextColor(getResources().getColor(R.color.white));
                liunianji.setTextColor(getResources().getColor(R.color.shouye_tab_color_selected));
                if (xueji.equals("1")) {
                    yinianji.setText("一年级");
                    ernianji.setText("二年级");
                    sannianji.setText("三年级");
                    sinianji.setText("四年级");
                    wunianji.setText("五年级");
                    liunianji.setText("六年级");
                } else if (xueji.equals("2")) {
                    nianji_buttom.setVisibility(View.GONE);
                    yinianji.setText("初一");
                    ernianji.setText("初二");
                    sannianji.setText("初三");

                } else if (xueji.equals("3")) {
                    nianji_buttom.setVisibility(View.GONE);
                    yinianji.setText("高一");
                    ernianji.setText("高二");
                    sannianji.setText("高三");
                }
                break;
            case R.id.liunianji:
                yinianji.setBackgroundResource(R.drawable.chazhao_pop_btn);
                ernianji.setBackgroundResource(R.drawable.chazhao_pop_btn);
                sannianji.setBackgroundResource(R.drawable.chazhao_pop_btn);
                sinianji.setBackgroundResource(R.drawable.chazhao_pop_btn);
                wunianji.setBackgroundResource(R.drawable.chazhao_pop_btn);
                liunianji.setBackgroundResource(R.drawable.zhibo_sey_btn_xuanzhong);
                yinianji.setTextColor(getResources().getColor(R.color.shouye_tab_color_selected));
                ernianji.setTextColor(getResources().getColor(R.color.shouye_tab_color_selected));
                sannianji.setTextColor(getResources().getColor(R.color.shouye_tab_color_selected));
                sinianji.setTextColor(getResources().getColor(R.color.shouye_tab_color_selected));
                wunianji.setTextColor(getResources().getColor(R.color.shouye_tab_color_selected));
                liunianji.setTextColor(getResources().getColor(R.color.white));
                if (xueji.equals("1")) {
                    yinianji.setText("一年级");
                    ernianji.setText("二年级");
                    sannianji.setText("三年级");
                    sinianji.setText("四年级");
                    wunianji.setText("五年级");
                    liunianji.setText("六年级");
                } else if (xueji.equals("2")) {
                    nianji_buttom.setVisibility(View.GONE);
                    yinianji.setText("初一");
                    ernianji.setText("初二");
                    sannianji.setText("初三");

                } else if (xueji.equals("3")) {
                    nianji_buttom.setVisibility(View.GONE);
                    yinianji.setText("高一");
                    ernianji.setText("高二");
                    sannianji.setText("高三");
                }
                break;
            case R.id.live_prepare_btn:
                ToastUtil.showMessage(getActivity(), "暂无内容");
//                Intent intent = new Intent(getActivity(), ScreenActivity.class);
//                intent.putExtra("subject",subject);
//                intent.putExtra("grade",grade);
//                startActivity(intent);
                break;
            case R.id.zhuanyun_back:
                firstSetPop.dismiss();
                break;
        }
    }

    private TextView xiaoxue, chuzhong, gaozhong, yuwen, shuxue, yingyu, zonghe, yinianji, ernianji, sannianji, sinianji, wunianji, liunianji;
    private Button start_btn;
    private TextView back;
    private LinearLayout nianji_buttom;

    private void setPopWindow() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.first_set_pop, null);
        nianji_buttom = (LinearLayout) view.findViewById(R.id.nianji_buttom);
        start_btn = (Button) view.findViewById(R.id.live_prepare_btn);
        start_btn.setOnClickListener(this);
        back = (TextView) view.findViewById(R.id.zhuanyun_back);
        back.setOnClickListener(this);
        xiaoxue = (TextView) view.findViewById(R.id.xiaoxue);
        xiaoxue.setOnClickListener(this);
        chuzhong = (TextView) view.findViewById(R.id.chuzhong);
        chuzhong.setOnClickListener(this);
        gaozhong = (TextView) view.findViewById(R.id.gaozhong);
        gaozhong.setOnClickListener(this);
        yuwen = (TextView) view.findViewById(R.id.yuwen);
        yuwen.setOnClickListener(this);
        shuxue = (TextView) view.findViewById(R.id.shuxue);
        shuxue.setOnClickListener(this);
        yingyu = (TextView) view.findViewById(R.id.yingyu);
        yingyu.setOnClickListener(this);
        zonghe = (TextView) view.findViewById(R.id.zonghe);
        zonghe.setOnClickListener(this);
        yinianji = (TextView) view.findViewById(R.id.yinianji);
        yinianji.setOnClickListener(this);
        ernianji = (TextView) view.findViewById(R.id.ernianji);
        ernianji.setOnClickListener(this);
        sannianji = (TextView) view.findViewById(R.id.sannianji);
        sannianji.setOnClickListener(this);
        sinianji = (TextView) view.findViewById(R.id.sinianji);
        sinianji.setOnClickListener(this);
        wunianji = (TextView) view.findViewById(R.id.wunianji);
        wunianji.setOnClickListener(this);
        liunianji = (TextView) view.findViewById(R.id.liunianji);
        liunianji.setOnClickListener(this);
        firstSetPop = new PopupWindow(view, WindowManager.LayoutParams.FILL_PARENT, WindowManager.LayoutParams.FILL_PARENT, true);
//        WindowManager.LayoutParams lp = getWindow().getAttributes();
//        lp.alpha = 0.5f; //0.0-1.0
//        getWindow().setAttributes(lp);
//        ColorDrawable dw = new ColorDrawable(0x60000000);
        firstSetPop.setBackgroundDrawable(new ColorDrawable());
        firstSetPop.showAtLocation(first_list_rl, Gravity.CENTER, 0, 0);
        firstSetPop.setOutsideTouchable(true);
        firstSetPop.setFocusable(false);
    }

    @Override
    public void onScroll(AbsListView listView, int firstVisiableItem, int visiableItemCount, int totalItemCount) {
//        if (xuanzeLayout == null) return;
        if (mListView.getHeaderViewsCount() > 0) {
            firstVisiableItem--;
        }
        firstListItem = firstVisiableItem;
        if (firstVisiableItem >= 1) {
//            xuanzeLayout.setVisibility(View.VISIBLE);
        } else {
//            xuanzeLayout.setVisibility(View.GONE);
        }
    }

    @Override
    public void onScrollStateChanged(AbsListView listView, int state) {

    }

    @Override
    public void onRefresh() {
        currentPage = 1;
        noDataUtil.ListViewNoEmpty(mListView);
        getTuijianList();
    }

    @Override
    public void onPullRefresh() {

    }

    @Override
    public void onLoadMore() {
        getTuijianList();
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("ZhiBoFragment");
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!TextUtils.isEmpty(EatShowApp.getACache().getAsString(Constants.USE_GUANKANQUAN))) {
            String isUse = EatShowApp.getACache().getAsString(Constants.USE_GUANKANQUAN);
            if (isUse.equals("true")) {
                getTuijianList();
            }
        }
        getUserDetail();
        if (isShow) {
            mListView.doRefresh();
        }
        MobclickAgent.onPageStart("ZhiBoFragment");
    }
}
